//
//  RGXAwardDetailController.m
//  JiCaiBao
//
//  Created by rgx on 16/11/7.
//  Copyright © 2016年 rgx. All rights reserved.
//

#import "RGXAwardDetailController.h"
#import "awardDetailCell.h"
@interface RGXAwardDetailController (){
    
    NSMutableArray *orderDataArr;
}

@end

@implementation RGXAwardDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self setShowTitle:@"期次详情"];
    [self initLeftNavigationBarItem];
    self.awardDetailTB.delegate = self;
    self.awardDetailTB.dataSource = self;
    
    self.awardDetailTB.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];

    // 马上进入刷新状态
    [self.awardDetailTB.mj_header beginRefreshing];
    
    self.awardDetailTB.tableHeaderView = self.headView;
}

- (void)loadNewData{
    
    [self queryPrizeDetail];
    [self.awardDetailTB.mj_header endRefreshing];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)queryPrizeDetail
{
    __weak RGXAwardDetailController *selfInfo = self;
    
    
    NSMutableDictionary *body = [NSMutableDictionary dictionary];
    [body setObject:self.gameId forKey:@"gameId"]; //彩种名称，多个用逗号隔开
    [body setObject:self.issueNo forKey:@"issueNo"];
    AFSecurityPolicy *securityPolicy = [[AFSecurityPolicy alloc] init];
    [securityPolicy setAllowInvalidCertificates:YES];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager POST:QueryPrizeDetail parameters:body progress:^(NSProgress * uploadProgress) {
        
    } success:^(NSURLSessionDataTask * task, id responseObject) {
        
        // 请求成功
        if(responseObject){
            RGXLog(@"responseObject: 开奖详情 = %@",responseObject);
            [selfInfo handleAwardDetailData:responseObject];
        } else {
            RGXLog(@"无数据");
        }
        
    } failure:^(NSURLSessionDataTask * task, NSError * error) {
        RGXLog(@"error = %@",error);
    }];
}

- (void)handleAwardDetailData:(NSDictionary *)data{
    
    if (![data isKindOfClass:[NSDictionary class]]) {
        return;
    }
    if (![[data objectForKey:@"statusCode"] isEqualToString:@"0000"]) {
        return;
    }
    NSArray *listArr = [[data objectForKey:@"body"] objectForKey:@"prizeLevels"];
    
    if (listArr.count == 0) {
        return;
    }
    if (listArr.count == 0) {
        [self.awardDetailTB reloadData];
        return;
    }
    if (!orderDataArr) {
        orderDataArr = [[NSMutableArray alloc]init];
    }else{
        [orderDataArr removeAllObjects];
    }
    
    if ([[data objectForKey:@"body"] objectForKey:@"issueNo"]) {
        self.issueNoLab.text = [NSString stringWithFormat:@"第 %@ 期",[[data objectForKey:@"body"] objectForKey:@"issueNo"]];
    }
    if ([[data objectForKey:@"body"] objectForKey:@"prizeTime"]) {
        NSString *prizeTime = [NSString stringWithFormat:@"%@",[[data objectForKey:@"body"] objectForKey:@"prizeTime"]];
        self.prizeTimeLab.text = [NSString stringWithFormat:@"%@",[self getTimeStr:prizeTime]];
    }
    if ([[data objectForKey:@"body"] objectForKey:@"prizePool"]) {
        self.prizePoolLab.text = [NSString stringWithFormat:@"%@",[[data objectForKey:@"body"] objectForKey:@"prizePool"]];
    }
    if ([[data objectForKey:@"body"] objectForKey:@"saleMoney"]) {
        self.saleMoneyLab.text = [NSString stringWithFormat:@"%@",[[data objectForKey:@"body"] objectForKey:@"saleMoney"]];
    }
    NSDictionary *prizeBalls = [[data objectForKey:@"body"] objectForKey:@"prizeBalls"];
    if (prizeBalls) {
        NSArray *redBalls = [prizeBalls objectForKey:@"red"];
        NSArray *blueBalls = [prizeBalls objectForKey:@"blue"];
        NSString *tag = @"";

        CGRect rect;
        int bx = 0;
        if ([self.gameId isEqualToString:lottery_ssq_id]) {
            for (int i = 0; i < 6; i++) {
                if (redBalls.count == 6) {
                    tag = [redBalls objectAtIndex:i];
                }
                bx = (kScreenWidth-7*35-6*3)/2+i*35+3*i;
                rect = CGRectMake(bx, 14, 35, 35);
                ballView *redBall = [[ballView alloc]initWithFrame:rect ballType:ballViewType_Red delegate:self tag:tag need:YES];
                
                [self.ballView addSubview:redBall];
            }
            if (blueBalls) {
                rect =CGRectMake(bx+35+3, 14, 35, 35);
                tag = [NSString stringWithFormat:@"%@",[blueBalls objectAtIndex:0]];
                ballView *blueBall = [[ballView alloc]initWithFrame:rect ballType:ballViewType_blue delegate:self tag:tag need:YES];
                [self.ballView addSubview:blueBall];
            }
        }else if ([self.gameId isEqualToString:lottery_fc3d_id]){
            
            for (int i = 0; i < 3; i++) {
                if (redBalls.count == 3) {
                    tag = [redBalls objectAtIndex:i];
                }
                bx = (kScreenWidth-3*35-2*10)/2+i*35+10*i;
                rect = CGRectMake(bx, 14, 35, 35);
                ballView *redBall = [[ballView alloc]initWithFrame:rect ballType:ballViewType_Red delegate:self tag:tag need:YES];
                
                [self.ballView addSubview:redBall];
            }
        
        }else if ([self.gameId isEqualToString:lottery_qlc_id]){
            
            for (int i = 0; i < 7; i++) {
                if (redBalls.count == 7) {
                    tag = [redBalls objectAtIndex:i];
                }
                bx = (kScreenWidth-8*35-7*3)/2+i*35+3*i;
                rect = CGRectMake(bx, 14, 35, 35);
                ballView *redBall = [[ballView alloc]initWithFrame:rect ballType:ballViewType_Red delegate:self tag:tag need:YES];
                
                [self.ballView addSubview:redBall];
            }
            if (blueBalls) {
                rect =CGRectMake(bx+35+3, 14, 35, 35);
                tag = [NSString stringWithFormat:@"%@",[blueBalls objectAtIndex:0]];
                ballView *blueBall = [[ballView alloc]initWithFrame:rect ballType:ballViewType_blue delegate:self tag:tag need:YES];
                [self.ballView addSubview:blueBall];
            }
        }
    }
    for (NSDictionary *dict in listArr) {
        awardDetailData *data = [[awardDetailData alloc]init];
        data.name  = [dict objectForKey:@"name"];
        data.count = [NSString stringWithFormat:@"%@",[dict objectForKey:@"count"]];
        data.money = [NSString stringWithFormat:@"%@",[dict objectForKey:@"money"]];
        [orderDataArr addObject:data];
    }
    [self.awardDetailTB reloadData];
}
#pragma mark - UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (orderDataArr.count) {
        return orderDataArr.count;
    }else{
        return 1;
    }
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 
    NSString *strid =@"awardDetail";
    awardDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:strid];
    if(cell ==nil){
        
        NSArray *arr = [[NSBundle mainBundle]loadNibNamed:@"awardDetailCell" owner:self options:nil];
        cell = [arr lastObject];
    }
    
    awardDetailData *data = [[awardDetailData alloc]init];
    
    if (orderDataArr && indexPath.row > 0) {
        data = [orderDataArr objectAtIndex:indexPath.row-1];
        [cell flashAwardDetailCell:data];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //隐藏没用的cell
    tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44.0;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
